package com.briup.app12.service.impl;

import com.briup.app12.bean.Account;
import com.briup.app12.dao.AccountMapper;
import com.briup.app12.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AccountServiceImpl implements IAccountService {
    @Resource
    private AccountMapper accountMapper;

    @Override
    public void save(Account account) {
        accountMapper.insert(account);
    }

    @Override
    public List<Account> findAll() {
        return accountMapper.selectAll();
    }

    @Override
    public Account findById(long id) {
        return accountMapper.selectByPrimaryKey(id);
    }

    @Override
    public Account findByAccount(String account) {
        return accountMapper.selectByAccount(account);
    }

    @Override
    public String saveOrUpdate(Account account) {
        if (account.getId() != null) {
            accountMapper.updateByPrimaryKey(account);
            return "更新成功";
        }
        if(findByAccount(account.getAccount()) != null){
            throw new RuntimeException("账号名已经被占用！");
        }
        accountMapper.insert(account);
        return "保存成功";
    }

    @Override
    public void deleteById(long id) {
        Account account = this.findById(id);
        if(account == null){
            throw new RuntimeException("该账号不存在");
        }
        accountMapper.deleteByPrimaryKey(id);
    }
}
